package org.xxx.epms.result.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.xxx.epms.common.api.CommonPage;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.result.dto.*;
import org.xxx.epms.result.feign.InspectorFeign;
import org.xxx.epms.result.feign.MonitorFeign;
import org.xxx.epms.result.models.AqiMeasuredResult;
import org.xxx.epms.result.service.AqiMeasuredResultService;
import org.xxx.epms.result.service.GridCityService;
import org.xxx.epms.result.service.GridProvinceService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/result")
public class AqiMeasuredResultController {
    @Autowired
    private AqiMeasuredResultService aqiMeasuredResultService;
    @Qualifier("org.xxx.epms.result.feign.InspectorFeign")
    @Autowired
    private InspectorFeign inspectorFeign;
    @Qualifier("org.xxx.epms.result.feign.MonitorFeign")
    @Autowired
    private MonitorFeign monitorFeign;

    @Autowired
    private GridProvinceService gridProvinceService;
    @Autowired
    private GridCityService gridCityService;

    //获取反馈列表总页数
    @RequestMapping(value = "/getMeasuredResultListTotalPage",method = RequestMethod.POST )
    @ApiOperation(value = "获取确认列表总页数", notes = "获取确认列表总页数")
    public CommonResult<Integer> getMeasuredResultListTotalPage(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        int totalPage = aqiMeasuredResultService.getTotalPage(aqiMeasuredResultParam, pageSize);
        System.out.println(totalPage);
        return CommonResult.success(totalPage);
    };

    //分页获取反馈列表
    @RequestMapping(value="/getMeasuredResultList", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取确认列表", notes = "分页获取确认列表")
    public CommonResult<CommonPage<AqiMeasuredResult>> getMeasuredResultList(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiMeasuredResult> page = aqiMeasuredResultService.getPageList(aqiMeasuredResultParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping(value="/getMeasuredResultDetail", method = RequestMethod.POST)
    @ApiOperation(value = "获取带有网格员信息的确认详情", notes = "获取带有网格员信息的确认详情")
    public CommonResult<CommonPage<AqiMeasuredResultDetailDTO>> getMeasuredResultDetail(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiMeasuredResult> aqiMeasuredResultPage = aqiMeasuredResultService.getPageList(aqiMeasuredResultParam, pageSize, pageNum);
        System.out.println(aqiMeasuredResultPage.getRecords());
        Page<AqiMeasuredResultDetailDTO> page = new Page<>();
        List<AqiMeasuredResultDetailDTO> list = new ArrayList<>();
        for (AqiMeasuredResult aqiMeasuredResult : aqiMeasuredResultPage.getRecords()) {
            AqiMeasuredResultDetailDTO dto = new AqiMeasuredResultDetailDTO();
            dto.setAqiMeasuredResult(aqiMeasuredResult);
            dto.setAqiValue(aqiMeasuredResultService.getAqiByAqimrId(aqiMeasuredResult.getAqimrId()));
            dto.setInspector(inspectorFeign.getInspectorByInsId(aqiMeasuredResult.getInsId()));
            dto.setMonitor(monitorFeign.getMonitorByMonId(String.valueOf(aqiMeasuredResult.getMonId())));
            dto.setGridProvince(gridProvinceService.getGridProvinceById(aqiMeasuredResult.getProvinceId()));
            dto.setGridCity(gridCityService.getGridCityById(aqiMeasuredResult.getCityId()));
            list.add(dto);
        }
        page.setRecords(list);
        System.out.println(list);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping(value="/createAqiMeasuredResult", method = RequestMethod.POST)
    @ApiOperation(value = "创建AQI测量结果",notes = "创建AQI测量结果")
    public CommonResult<Integer> createAqiMeasuredResult(@RequestBody AqiMeasuredResult aqiMeasuredResult){
        return CommonResult.success(aqiMeasuredResultService.createAqiMeasuredResult(aqiMeasuredResult));
    }

    @RequestMapping(value="/updateAqiMeasuredResult", method = RequestMethod.POST)
    @ApiOperation(value = "更新AQI测量结果",notes = "更新AQI测量结果")
    public CommonResult<Integer> updateMeasuredResult(@RequestBody AqiMeasuredResult aqiMeasuredResult){
        return CommonResult.success(aqiMeasuredResultService.updateAqiMeasuredResult(aqiMeasuredResult));
    }

    @ApiOperation(value = "获取所有省份的AQI超标结果",notes = "获取所有省份的AQI超标结果")
    @RequestMapping(value = "/listProvinceGrouping", method = RequestMethod.POST)
    public CommonResult<List<ProvinceGroupingDTO>> listProvinceGrouping(){
        List<ProvinceGroupingDTO> list = aqiMeasuredResultService.listProvinceGrouping();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取AQI测量结果分布情况",notes = "获取AQI测量结果分布情况")
    @RequestMapping(value = "/getAqiDistribution", method = RequestMethod.GET)
    public CommonResult<List<AqiDistributionDTO>> getAqiDistribution() {
        return CommonResult.success(aqiMeasuredResultService.getAqiDistribution());
    }

    @ApiOperation(value = "AQI空气质量指数超标趋势",notes = "AQI空气质量指数超标趋势")
    @RequestMapping(value = "/getAqiTrend", method = RequestMethod.GET)
    public CommonResult<List<AqiTrendDTO>> getAqiTrend() {
        return CommonResult.success(aqiMeasuredResultService.getAqiTrend());
    }

    @ApiOperation(value = "获取AQI已测试结果数",notes = "获取AQI已测试结果数")
    @RequestMapping(value = "/getAqiMeasuredResultCount", method = RequestMethod.GET)
    public CommonResult<Integer> getAqiMeasuredResultCount() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultCount());
    }

    @ApiOperation(value = "获得AQI空气质量指数良好总数",notes = "获得AQI空气质量指数良好总数")
    @RequestMapping(value = "/getAqiGoodCount", method = RequestMethod.GET)
    public CommonResult<Integer> getAqiGoodCount() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultGoodCount());
    }

    @ApiOperation(value = "获得AQI省份覆盖率",notes = "获得AQI省份覆盖率")
    @RequestMapping(value = "/getAqiProvinceCoverage", method = RequestMethod.GET)
    public CommonResult<String> getAqiProvinceCoverage() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultProvinceCov());
    }


    @ApiOperation(value = "获得AQI城市覆盖率",notes = "获得AQI城市覆盖率")
    @RequestMapping(value = "/getAqiCityCoverage", method = RequestMethod.GET)
    public CommonResult<String> getAqiCityCoverage() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultCityCov());
    }

    @ApiOperation(value = "其他数据统计",notes = "其他数据统计")
    @RequestMapping(value = "/getOtherData", method = RequestMethod.GET)
    public CommonResult<OtherDataDTO> getOtherData() {
        OtherDataDTO otherData = new OtherDataDTO();
        otherData.setCount(aqiMeasuredResultService.getAqiMeasuredResultCount());
        otherData.setGoodCount(aqiMeasuredResultService.getAqiMeasuredResultGoodCount());
        otherData.setProvinceCoverage(aqiMeasuredResultService.getAqiMeasuredResultProvinceCov()+"%");
        otherData.setCityCoverage(aqiMeasuredResultService.getAqiMeasuredResultCityCov()+"%");
        return CommonResult.success(otherData);
    }

    @RequestMapping(value="/getMeasuredResultByFbId", method = RequestMethod.GET)
    @ApiOperation(value = "根据反馈id获取确认详情", notes = "根据反馈id获取确认详情")
    public CommonResult<AqiMeasuredResultDetailDTO> getMeasuredResultByFbId(@RequestParam(value = "fbId") Integer fbId){
        return CommonResult.success(aqiMeasuredResultService.getMeasuredResultByFbId(fbId));
    }

    @RequestMapping(value = "/getMeasuredResultByInsId",method = RequestMethod.GET )
    @ApiOperation(value = "根据insId获取确认信息", notes = "根据insId获取确认信息")
    public CommonResult<List<AqiMeasuredResultDetailDTO>>  getMeasuredResultByInsId(@RequestParam(value = "insId") Integer insId){
        return CommonResult.success(aqiMeasuredResultService.getMeasuredResultByInsId(insId));
    }
}
